package com.farsunset.ichat.example.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.farsunset.cim.client.android.CIMConnectorManager;
import com.farsunset.cim.client.android.CIMConnectorService;
import com.farsunset.cim.client.android.CIMSessionDisableException;
import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.ichat.example.R;
import com.farsunset.ichat.example.app.CIMMonitorActivity;
import com.farsunset.ichat.example.app.Constant;
import com.farsunset.ichat.example.util.StringUtil;

public class LoginActivity extends CIMMonitorActivity implements
		OnClickListener {

	EditText accountEdit;
	Button loginButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initViews();
	}

	private void initViews() {

		accountEdit = (EditText) this.findViewById(R.id.account);
		loginButton = (Button) this.findViewById(R.id.login);
		loginButton.setOnClickListener(this);

	}

	private void doLogin() {

		if (!"".equals(accountEdit.getText().toString().trim())) {
			SentBody sent = new SentBody();
			sent.setKey(CIMConstant.RequestKey.CLIENT_AUTH);
			sent.put("account", accountEdit.getText().toString().trim());
			sent.put("deviceId", StringUtil.getIMEI(LoginActivity.this));
			sent.put("channel", "android");
			showProgressDialog("提示", "正在登陆，请稍后......");
			CIMConnectorManager.getManager(this).send(sent);
		}

	}

	@Override
	public void onReplyReceived(final ReplyBody reply) {

		if (reply.getKey().equals(CIMConstant.RequestKey.CLIENT_AUTH)) {

			if (reply.getCode().equals(CIMConstant.ReturnCode.CODE_200)) {

				hideProgressDialog();
				Intent intent = new Intent(this, SystemMessageActivity.class);
				intent.putExtra("account", accountEdit.getText().toString().trim());
				startActivity(intent);
				this.finish();
			}
		}

	}

	@Override
	public void onConnectionSucceed() {

		doLogin();
	}

	@Override
	public void onConnectionFailed(Exception e) {

		hideProgressDialog();
		showToask("网络连接失败");

	}

	//发送请求失败时
	@Override
	public void onSentFailed(Exception e, SentBody sent) {

		//连接关闭，重新连接服务端
		if (e instanceof CIMSessionDisableException) {
			CIMConnectorManager.getManager(this).connect(Constant.CIM_SERVER_HOST, Constant.CIM_SERVER_PORT);
		} else {
			hideProgressDialog();
			showToask("登录失败,请重试!");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.login:
			doLogin();
			break;
		}

	}

	@Override
	public void onBackPressed() {

		stopService(new Intent(this, CIMConnectorService.class));
		CIMConnectorManager.destroy();
		this.finish();
	}
}
